using Engine;
using Game;
using SCIENEW.Tech;

namespace SCIENEW {
    public class TechTreeNodeWidget : CanvasWidget {
        public BevelledButtonWidget BevelledButtonWidget;
        public TechTreeBlockIconWidget BlockIconWidget;
        public TechNode TechNode;
        public Action<TechNode> OnClicked = node => { };

        public bool IsChecked {
            get => BevelledButtonWidget.IsChecked;
            set => BevelledButtonWidget.IsChecked = value;
        }

        bool m_inited = false;

        public TechTreeNodeWidget(TechNode techNode) {
            Size = new Vector2(72f);
            TechNode = techNode;
            ClampToBounds = true;
            BevelledButtonWidget = new BevelledButtonWidget {
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center,
                IsAutoCheckingEnabled = true
            };
            Children.Add(BevelledButtonWidget);
            BlockIconWidget = new TechTreeBlockIconWidget {
                HorizontalAlignment = WidgetAlignment.Center,
                VerticalAlignment = WidgetAlignment.Center,
                Value = techNode.IconBlock,
                Size = new Vector2(48f),
                IsHitTestVisible = false,
            };
            Children.Add(BlockIconWidget);
        }

        public override void Update() {
            base.Update();
            if (BevelledButtonWidget.IsClicked) OnClicked(TechNode);
            BevelledButtonWidget.BevelColor = BevelledButtonWidget.CenterColor = GetColor();
            if (!m_inited) {
                m_inited = true;
                Task.Run(() => {Thread.Sleep(100);//延迟刷新，防止覆盖
                    BlockIconWidget.m_needRefresh = true;
                });
            }

            Color GetColor() {
                switch (TechNode.GetMode()) {
                    case TechNode.Mode.Locked: return new Color(181, 172, 154);
                    case TechNode.Mode.Handling: return new Color(250, 220, 90);
                    case TechNode.Mode.Unlocked: return new Color(100, 172, 200);
                }
                return Color.White;
            }
        }
    }
}